package com.market.controller;

import com.market.service.ShopCartService;
import com.market.utils.TokenUtil;
import com.market.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("shopcart")
public class ShopCartController {
    @Autowired
    private ShopCartService shopCartService;

    @RequestMapping("reduceShopCart")
    public R reduceShopCart(HttpServletRequest request,int goodsId){
        Map<String, Object> map = TokenUtil.parseToken(request.getHeader("token"));
        int id =Integer.parseInt((String) map.get("id"));
        boolean b = shopCartService.reduceShopCartNums(id, goodsId);
        if (b){
            return new R(220,"减少购物车商品数量成功",null);
        }
        return new R(330,"减少购物车商品数量失败",null);
    }

    @RequestMapping("increaseShopCart")
    public R increaseShopCart(HttpServletRequest request,int goodsId){
        Map<String, Object> map = TokenUtil.parseToken(request.getHeader("token"));
        int id =Integer.parseInt((String) map.get("id"));
        boolean b = shopCartService.addShopCartNums(id, goodsId);
        if (b){
            return new R(221,"增加购物车商品数量成功",null);
        }
        return new R(331,"增加购物车商品数量失败",null);
    }

    @RequestMapping("updateShopCart")
    public R updateShopCart(HttpServletRequest request,int goodsId,int nums){
        Map<String, Object> map = TokenUtil.parseToken(request.getHeader("token"));
        int id =Integer.parseInt((String) map.get("id"));
        boolean b = shopCartService.updateShopCart(nums, id, goodsId);
        if (b){
            return new R(222,"修改购物车商品数量成功",null);
        }
        return new R(332,"修改购物车商品数量失败",null);
    }
}
